package cn.edu.hstc.wm.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;
import org.ksoap2.serialization.SoapObject;

import android.util.Log;
import cn.edu.hstc.wm.bean.Good;

/**
 * 商品提供服务程序服务
 */
public class GoodService {

	/**
	 * 根据传入的请求 和参数获取数据
	 * 
	 * @param soapAction
	 * @param params
	 * @return
	 */
	private static List<Good> getGoodsListByMethod(String soapAction, List<BasicNameValuePair> params) {

		SoapObject resultObject = null;
		try {
			resultObject = SoapService.getSoapObject(soapAction, params);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

		// Log.d("resultObject", resultObject.toString());
		return parseData(resultObject);
	}

	/**
	 * 从soapObject 消息中获取到数据,解析数据并生成集合对象返回
	 * 
	 * @param resultObject
	 * @return
	 */
	private static List<Good> parseData(SoapObject resultObject) {
		// 解析服务器响应的soap消息
		List<Good> list = new ArrayList<Good>();
		for (int i = 0; i < resultObject.getPropertyCount(); i++) {
			SoapObject childObject = (SoapObject) resultObject.getProperty(i);
			// Log.d("childObject", "" + childObject.toString());
			Good good = new Good();
			// 解析Good
			good.setId(Integer.parseInt(childObject.getPropertyAsString("id")));
			good.setImageName(childObject.getPropertyAsString("imageName"));
			good.setName(childObject.getPropertyAsString("name"));
			good.setPrice(Float.parseFloat(childObject.getPropertyAsString("price")));
			list.add(good);
		}
		return list;
	}

	/**
	 * 根据店铺id来获取该店铺所有的good的个数
	 * 
	 * @param id
	 * @return 该店铺所有商品总数目
	 * @throws Exception
	 */
	public static int getGoodsCountByStoreId(int id) throws Exception {
		String soapAction = "getGoodsCountByStoreId";
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("storeId", "" + id));
		SoapObject resultObject = SoapService.getSoapObject(soapAction, params);
		// Log.d("resultObject", resultObject.toString());
		return Integer.parseInt(resultObject.getProperty(0).toString());
	}

	/**
	 * 获取所有的Good
	 * 
	 * @return 查找所有商品
	 * @throws Exception
	 */
	public static List<Good> getAllGoods() throws Exception {
		String methodAction = "getAllGoods";
		return getGoodsListByMethod(methodAction, null);
	}

	/**
	 * 分页查找商品
	 * 
	 * @param firstResult
	 *            起始位置
	 * @param maxResults
	 *            查找个数
	 * @return
	 * @throws Exception
	 */
	public static List<Good> getGoods(int firstResult, int maxResults) throws Exception {
		String methodAction = "getGoods";
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("firstResult", "" + firstResult));
		params.add(new BasicNameValuePair("maxResults", "" + maxResults));
		return getGoodsListByMethod(methodAction, params);
	}

	/**
	 * 根据商店id查找拥有的商品。
	 * 
	 * @param id
	 *            店铺的id
	 * @return
	 * @throws Exception
	 */
	public static List<Good> getGoodsByStoreId(int id) throws Exception {
		String methodAction = "getGoodsByStoreId";
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("id", "" + id));
		return getGoodsListByMethod(methodAction, params);
	}

	/**
	 * 指定店铺里一定数量的Good
	 * 
	 * @param id
	 *            店铺id
	 * @param firstResult
	 *            起始位置
	 * @param maxResults
	 *            个数
	 * @return
	 * @throws Exception
	 */
	public static List<Good> getGoodsList(int id, int firstResult, int maxResults) throws Exception {
		String methodAction = "getGoodsList";
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("storeId", "" + id));
		params.add(new BasicNameValuePair("firstResult", "" + firstResult));
		params.add(new BasicNameValuePair("maxResults", "" + maxResults));
		return getGoodsListByMethod(methodAction, params);
	}
}
